package com.iqismart.shorturl.controller.api.get;

import com.iqismart.shorturl.service.LogService;
import com.iqismart.shorturl.service.UrlService;
import com.iqismart.shorturl.vo.UrlResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.TimeZone;

/**
 * @Description
 * @Auther sty
 * @createTime 2019/10/14 01:32
 */
@Controller
@CrossOrigin
public class GetController {

    @Autowired
    private UrlService urlService;

    @Autowired
    private LogService logService;

    @RequestMapping("/{shortUrl}")
    public ModelAndView index(@PathVariable("shortUrl")  String shortUrl, HttpServletRequest req, HttpServletResponse response){
        try {
            UrlResponse urlResponse = urlService.getLongUrl(shortUrl);

            //密码
            if(StringUtils.isNotEmpty(urlResponse.getPassword())){
                //设置了密码
                return new ModelAndView("s","vo",urlResponse);
            }

            if(urlResponse.getExpireAt()>0 && urlResponse.getExpireAt()<System.currentTimeMillis()){
                //已过期
                ModelAndView modelAndView = new ModelAndView("e");
                modelAndView.addObject("title","已过期");
                modelAndView.addObject("message","该链接已经过期了！");
                return modelAndView;
            }

            //次数 或 ip 限制
            if(urlResponse.getCount() > 0){
                //查看该链接的PV 和 IP 数
                if(urlResponse.getCountUnit().toUpperCase().equals("IP")){
                    Long ip = logService.getIP(urlResponse.getId());
                    if(ip > urlResponse.getCount()){
                        ModelAndView modelAndView = new ModelAndView("e");
                        modelAndView.addObject("title","次数受限");
                        modelAndView.addObject("message","该链接访问IP数已达上限！");
                        return modelAndView;
                    }
                }else{
                    Long pv = logService.getPV(urlResponse.getId());
                    if(pv > urlResponse.getCount()){
                        ModelAndView modelAndView = new ModelAndView("e");
                        modelAndView.addObject("title","次数受限");
                        modelAndView.addObject("message","该链接访问次数已达上限！");
                        return modelAndView;
                    }
                }
            }

            if(StringUtils.isNotEmpty(urlResponse.getTarea())) {
                //private static String[][] oldMappings = new String[][]{{"ACT", "Australia/Darwin"}, {"AET", "Australia/Sydney"}, {"AGT", "America/Argentina/Buenos_Aires"}, {"ART", "Africa/Cairo"}, {"AST", "America/Anchorage"}, {"BET", "America/Sao_Paulo"}, {"BST", "Asia/Dhaka"}, {"CAT", "Africa/Harare"}, {"CNT", "America/St_Johns"}, {"CST", "America/Chicago"}, {"CTT", "Asia/Shanghai"}, {"EAT", "Africa/Addis_Ababa"}, {"ECT", "Europe/Paris"}, {"IET", "America/Indiana/Indianapolis"}, {"IST", "Asia/Kolkata"}, {"JST", "Asia/Tokyo"}, {"MIT", "Pacific/Apia"}, {"NET", "Asia/Yerevan"}, {"NST", "Pacific/Auckland"}, {"PLT", "Asia/Karachi"}, {"PNT", "America/Phoenix"}, {"PRT", "America/Puerto_Rico"}, {"PST", "America/Los_Angeles"}, {"SST", "Pacific/Guadalcanal"}, {"VST", "Asia/Ho_Chi_Minh"}};
                Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(urlResponse.getTimezone()));
                final int hour = cal.get(Calendar.HOUR_OF_DAY);
                final String[] split = urlResponse.getTarea().split("-");
                int start =Integer.parseInt(split[0]);
                int end =Integer.parseInt(split[1]);
                if(hour < start || hour > end){
                    //Aoo,该链接现在是不可访问时段,换个时段再来试试吧！
                    ModelAndView modelAndView = new ModelAndView("e");
                    modelAndView.addObject("title","暂不可访问");
                    modelAndView.addObject("message","该链接现在是不可访问时段,换个时段再来试试吧！");
                    return modelAndView;
                }
            }

            if(StringUtils.isNotEmpty(urlResponse.getLongUrl())) {
                response.sendRedirect(urlResponse.getLongUrl());
                return null;
            }else{
               req.getRequestDispatcher(shortUrl).forward(req,response);
            }
        } catch (Exception e) {
            ModelAndView modelAndView = new ModelAndView("e");
            modelAndView.addObject("title","404");
            modelAndView.addObject("message","找不到页面！");
            return modelAndView;
            /*try {
                response.sendRedirect("/");
            } catch (IOException e1) {
                e1.printStackTrace();
            }*/
        }
        return null;
    }


}
